package com.example.hssmgs.admin;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.hssmgs.R;
import com.example.hssmgs.Student.CommonDatabase;

@SuppressLint("Registered")
public class Admin_change_tch extends AppCompatActivity {
    EditText input_id_admin_tch;
    EditText input_name_admin_tch;
    Button revise_bt;
    Button back_bt;
    SQLiteDatabase db;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_change_tch);

        initView();


//        final String status = input_status_admin.getText().toString();

        revise_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id = input_id_admin_tch.getText().toString();

                if (id.equals("")){
                    Toast toast = Toast.makeText(getApplicationContext(),"学号/工号不能为空", Toast.LENGTH_SHORT);
                    toast.show();
                }else {
                    @SuppressLint("Recycle") Cursor cursor = db.query("teacher_db", null, "id = ?", new String[]{id}, null, null, null);

                    ContentValues values = new ContentValues();

                    values.put("name",input_name_admin_tch.getText().toString());

                    db.update("teacher_db", values, "id=?", new String[]{id});

                    Toast toast=Toast.makeText(getApplicationContext(),"修改成功",Toast.LENGTH_SHORT);
                    toast.show();
                    finish();
                }
            }
        });

        back_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        input_id_admin_tch=findViewById(R.id.input_id_admin_tch);
        input_name_admin_tch=findViewById(R.id.input_name_admin_tch);
        revise_bt=findViewById(R.id.revise_bt3);
        back_bt=findViewById(R.id.back_bt3);
        // 获取数据库对象
        CommonDatabase commonDatabase = new CommonDatabase();
        db = commonDatabase.getSqliteObject(Admin_change_tch.this, "test2.db");
    }
}
